package com.decent.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: 吃瓜
 * @Description:
 * @Date Created in 2020-10-09 11:06
 * @Modified By:
 */
public class CookiesUtil {
    public static void cookiesInvalidate(HttpServletResponse response, String... cookieNames){
        for (String cookieName : cookieNames){
            //一定要new一个新的同名Cookie , 添加进去
            Cookie c = new Cookie(cookieName,null);
            c.setMaxAge(0);
            response.addCookie(c);
        }
        System.out.println("CookiesInvalidate");
    }

    public static String getCookieValue(HttpServletRequest request, String cookieName) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            return null;
        }
        //cookies存在值
        String cookieValue = null;
        for (Cookie cookie : cookies){
            String name = cookie.getName();
            if (cookieName.equals(name)){
                cookieValue = cookie.getValue();
                break;
            }
        }
        return cookieValue;
    }
}
